public-obj-c=$(wildcard ./src/*.c)
public-obj-o=$(patsubst %.c, %.o, $(public-obj-c))
login-obj-c=$(wildcard ./login/*.c)
login-obj-o=$(patsubst %.c, %.o, $(login-obj-c))
CFLAGS=-g -Wall -I ./include
CC=arm-none-linux-gnueabi-gcc
AR=arm-none-linux-gnueabi-ar
RANLIB=arm-none-linux-gnueabi-ranlib
LIBS=-L./ -lcgic -lpthread -L ./lib -lsqlite3

all: libcgic.a cgictest.cgi capture

install: libcgic.a
	cp libcgic.a /usr/local/lib
	cp cgic.h /usr/local/include
	@echo libcgic.a is in /usr/local/lib. cgic.h is in /usr/local/include.

libcgic.a: ./src/cgic.o cgic.h
	rm -f libcgic.a
	$(AR) rc libcgic.a ./src/cgic.o
	$(RANLIB) libcgic.a

#mingw32 and cygwin users: replace .cgi with .exe

$(obj-o):%.o:%.c
	$(CC) -c $< -o $@ $(LDFLAGS) $(CFLAGS) 
	
$(login-obj-o):%.o:%.c
	$(CC) -c $< -o $@ $(LDFLAGS) $(CFLAGS) 

cgictest.cgi: cgictest.o libcgic.a
	$(CC) $(CFLAGS) cgictest.o -o cgictest.cgi ${LIBS}

capture: capture.o libcgic.a
	$(CC) $(CFLAGS) capture.o -o capture ${LIBS}

login.cgi: $(login-obj-o) $(public-obj-o) libcgic.a 
	$(CC) $(CFLAGS) $(login-obj-o) $(public-obj-o) -o login.cgi ${LIBS}

logout.cgi: logout.o libcgic.a session.o
	$(CC) $(CFLAGS) logout.o session.o -o logout.cgi ${LIBS}

ledc.cgi: ledc.o libcgic.a
	$(CC) $(CFLAGS) ledc.o -o ledc.cgi ${LIBS} --static
	
clean:
	rm -f *.o *.a cgictest.cgi capture cgicunittest

test:
	gcc -D UNIT_TEST=1 cgic.c -o cgicunittest
	./cgicunittest
